<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>公告更新子页面</title>
    <script src="../../js/jquery-3.4.1.min.js"></script>
    <script src="../../layui/layui.js"></script>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <script>
        $(function () {
            var parent_json = eval('('+parent.json+')');
            $("#post-title").val(parent_json.title);
            $("#post-content").val(parent_json.content);
        });
    </script>
</head>

<form class="layui-form" method="post" style="margin-top: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block" style="width: 340px">
            <input id="post-title" type="text" name="title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text" style="padding-right: 50px">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <textarea id="post-content" name="desc" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script>
    //Demo
    layui.use('form', function(){
        var form = layui.form;
        var parent_json = eval('('+parent.json+')');
        $("#post-title").val(parent_json.title);
        $("#post-content").val(parent_json.content);
        console.log(parent_json.id);
        //监听提交
        form.on('submit(formDemo)', function(data){
            $.ajax({
                url: '/posts',
                method: 'put',
                data: JSON.stringify({
                    id: parent_json.id,
                    title: $("#post-title").val(),
                    content: $("#post-content").val()
                }),
                contentType: "application/json",
                success: function (res) {
                    console.log(res);
                    if (res.code == 200) {
                        parent.layerCallback($("#post-title").val(), $("#post-content").val());
                        parent.layer.msg('修改公告成功', {icon: 1});
                    } else {
                        parent.layer.msg('修改公告失败', {icon: 2});
                    }
                }
            });
            return false;
        });
    });
</script>
</html>

